﻿using System;

namespace Xc.DataAccess.Core
{
    /// <summary>
    /// 执行结果
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public  class ResultData<T> : Result
    {
        public ResultData() 
        {

        }
        /// <summary>
        /// 结果
        /// </summary>
        public T Data { get; set; }
        /// <summary>
        /// 同意
        /// </summary>
        /// <param name="result"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static ResultData<T> Accept(string message, T result)
        {
            return new ResultData<T> { IsSuccess = true, Data = result, Message = message };
        }
        /// <summary>
        /// 拒绝
        /// </summary>
        /// <param name="result"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static ResultData<T> Reject(string message, T result)
        {
            return new ResultData<T> { IsSuccess = false, Data = result, Message = message };
        }
        /// <summary>
        /// 使用整数判断
        /// </summary>
        /// <param name="number"></param>
        /// <param name="validate"></param>
        /// <returns></returns>
        public static ResultData<TResult> From< TResult, T>(T number, Func<T, bool> validate,TResult result)
        {
            bool res = false;
            if (validate != null)
            {
                res = validate.Invoke(number);
            }
            else
            {
                res = number?.ToString() == default(T).ToString();
            }
            if (res == true)
            {
                return ResultData<TResult>.Accept("成功", result);
            }
            else
            {
                return ResultData<TResult>.Reject("失败", result);
            }
        }
    }
    /// <summary>
    /// 执行结果
    /// </summary>
    /// <typeparam name="TResult"></typeparam>
    /// <typeparam name="TInfos"></typeparam>
    public class ResultData<TResult,TInfos> : Result
    {
        protected ResultData()
        {

        }
        /// <summary>
        /// 结果
        /// </summary>
        public TResult Result { get; set; }
        /// <summary>
        /// 信息
        /// </summary>
        public TInfos Infos { get; set; }
        /// <summary>
        /// 同意
        /// </summary>
        /// <param name="result"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static ResultData<TResult, TInfos> Accept(string message, TResult result, TInfos infos)
        {
            return new ResultData<TResult, TInfos> { IsSuccess = true, Result = result, Infos = infos, Message = message };
        }
        /// <summary>
        /// 拒绝
        /// </summary>
        /// <param name="result"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static ResultData<TResult, TInfos> Reject(string message, TResult result=default,TInfos infos=default)
        {
            return new ResultData<TResult, TInfos> { IsSuccess = false,Result= result, Infos = infos, Message = message };
        }
    }
}
